﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Linq;
using Common;

namespace GadgetApplication
{
    public partial class Form1 : Form
    {
        private QueueHandler queueHandler;

        public Form1(QueueHandler queueHandler)
        {
            this.queueHandler = queueHandler;
            InitializeComponent();
        }

        private void itemBindingNavigatorSaveItem_Click(object sender, EventArgs e)
        {
            this.Validate();
            this.itemBindingSource.EndEdit();
            this.tableAdapterManager.UpdateAll(this._WGRUS_Gadget);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the '_WGRUS_Gadget.Item' table. You can move, or remove it, as needed.
            this.itemTableAdapter.Fill(this._WGRUS_Gadget.Item);
        }

        private void bt_Reload_Click(object sender, EventArgs e)
        {
            this.itemTableAdapter.Fill(this._WGRUS_Gadget.Item);
        }

        private void bt_Save_Click(object sender, EventArgs e)
        {
            this.itemTableAdapter.Update(this._WGRUS_Gadget.Item);
        }

        private void bt_SendCatalog_Click(object sender, EventArgs e)
        {
            XNamespace ns = @"http://gadget.com/WGRUS/catalog";

            var xmlcatalog =
                new XDocument();
            XElement rootElement = new XElement(ns+"Catalog");
            xmlcatalog.Add(rootElement);
            foreach (var item in _WGRUS_Gadget.Item)
            {
                var element = new XElement(ns+"Item",
                    new XElement(ns+"Id", item.Id),
                    new XElement(ns+"Beskrivelse", item.Beskrivelse),
                    new XElement(ns+"Pris", item.Pris)
                    );
                rootElement.Add(element);
            }

            var strXml = xmlcatalog.ToString();
            queueHandler.SendMessage(Constants.GADGET_CATALOG_QUEUE, strXml, null);
        }
    }
}